import { Bin2Sha256 } from "../functions/3rd/sha256-uint8array.js";
import { RectRange } from "../functions/image/rect.js";
import { Editor } from "./pixeleditor.js";

class HistoryItem {
    id: number;
    text: string;


    width: number;
    height: number;

    data: Uint8Array | Uint8ClampedArray;
    datahash: string;
    select: RectRange;
    dataselect: Uint8Array | Uint8ClampedArray;
    dataselecthash: string;
}
export class History {
    private editor: Editor;
    constructor(editor: Editor, maxstep: number) {
        this.editor = editor;

        this.maxstep = maxstep;
    }
    private maxstep: number;
    private _itemid: number = 1;
    private items: HistoryItem[] = []
    private activeItem: HistoryItem;
    GetActivate(): HistoryItem {
        return this.activeItem;
    }
    TrySave(txt: string): number {


        let item = new HistoryItem();
        item.id = this._itemid;
        item.text = txt;


        item.width = this.editor.drawCanvas.getWidth();
        item.height = this.editor.drawCanvas.getHeight();
        item.data = this.editor.drawCanvas.getCurrentLayer().getBitmapData().data;
        item.datahash = Bin2Sha256(item.data);
        let s = this.editor.drawCanvas.getCurrentLayer().getSelect();
        if (s != null) {
            item.select = s.Clone();
            if (this.editor.drawCanvas.getCurrentLayer().getBitmapSelect() == null) {
                this.editor.drawCanvas.getCurrentLayer().copyDataToSelect();
                item.dataselect = this.editor.drawCanvas.getCurrentLayer().getBitmapDataSelect().data;
                this.editor.drawCanvas.getCurrentLayer().dropSelectData();
            }
            else {
                item.dataselect = this.editor.drawCanvas.getCurrentLayer().getBitmapDataSelect().data;
            }
            item.dataselecthash = Bin2Sha256(item.dataselect);
        }

        //去重
        if (this.activeItem != null) {

            if (this.activeItem.datahash == item.datahash && this.activeItem.dataselecthash == item.dataselecthash)
                return -1;
        }

        let index = this.items.indexOf(this.activeItem);

        this.items.splice(index + 1);

        this.items.push(item);
        this._itemid++;

        this.activeItem = item;

        if (this.items.length > 20) {
            this.items.splice(0, 1);
        }
        console.log("History ADD:" + item.text + "len=" + this.items.length);


        this.editor.drawCanvas.RePaint();

        return item.id;
    }
    ApplyHistory(item: HistoryItem) {
        if (item == this.activeItem)
            return;
        if (this.items.indexOf(item) == -1)
            return;
        if (this.activeItem.datahash == item.datahash && this.activeItem.dataselecthash == item.dataselecthash)
            return;
        this.activeItem = item;
        this.editor.drawCanvas.getCurrentLayer().SetSelectData(this.activeItem.select, this.activeItem.dataselect);
        this.editor.drawCanvas.getCurrentLayer().SetEditData(this.activeItem.data);
        this.editor.drawCanvas.getCurrentLayer().updateBitmap();
        this.editor.drawCanvas.RePaint();
    }
    GoLast(): void {
        let index = this.items.indexOf(this.activeItem);
        let act: HistoryItem = null;
        if (index > 0) {
            act = this.items[index - 1];
        }
        else {
            return;
        }
        console.log("History now:" + this.items.indexOf(act) + " " + act.text);
        this.ApplyHistory(act);
    }
    GoNext(): void {
        let index = this.items.indexOf(this.activeItem);
        let act: HistoryItem = null;
        if (index < this.items.length - 1) {
            act = this.items[index + 1];
        }
        else { return; }
        console.log("History now:" + this.items.indexOf(act) + " " + act.text);

        this.ApplyHistory(act);
    }

}